package com.slipper.system.codeless.service;

import java.util.Collections;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.system.codeless.bo.DataTableFieldStyleBO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.codeless.dao.DataTableFieldStyleDao;

import com.slipper.util.Tool;
import com.slipper.util.UUID;

@Service
@ReadTransactional
public class DataTableFieldStyleService {
    @Resource
    private DataTableFieldStyleDao dataTableFieldStyleDao;

    public DataTableFieldStyleBO queryBy(String id) {
        return dataTableFieldStyleDao.queryBy(id);
    }

    public List<DataTableFieldStyleBO> queryList(DataTableFieldStyleBO bo) {
        return dataTableFieldStyleDao.queryList(bo);
    }

    public PageInfo<DataTableFieldStyleBO> queryPage(DataTableFieldStyleBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(dataTableFieldStyleDao.queryList(bo));
    }

    @WriteTransactional
    public int save(DataTableFieldStyleBO bo) {
        if (Tool.isNotBlank(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            return dataTableFieldStyleDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            return dataTableFieldStyleDao.insert(bo);
        }
    }

    @WriteTransactional
    public int save(String fieldId, List<DataTableFieldStyleBO> list, String employeeId) {
        if (Tool.isBlank(fieldId)) {
            return 0;
        }

        // 保存/更新数据
        int i = 0;
        for (DataTableFieldStyleBO style : list) {
            style.setFieldId(fieldId);
            style.setOrdinal(++i);
            if (Tool.isBlank(style.getId())) {
                style.setId(UUID.getUUID());
                style.setUpdateId(employeeId);
                style.setCreateId(employeeId);
                style.setUpdateTime(System.currentTimeMillis());
                style.setCreateTime(System.currentTimeMillis());
                dataTableFieldStyleDao.insert(style);
            } else {
                style.setUpdateId(employeeId);
                style.setUpdateTime(System.currentTimeMillis());
                dataTableFieldStyleDao.update(style);
            }
        }

        // 删除被删除的数据
        DataTableFieldStyleBO qo = new DataTableFieldStyleBO();
        qo.setFieldId(fieldId);
        dataTableFieldStyleDao.queryList(qo).stream()
                .filter(style -> list.stream().noneMatch(temp -> style.getId().equals(temp.getId())))
                .forEach(this::delete);
        return 1;
    }

    @WriteTransactional
    public void saveList(List<DataTableFieldStyleBO> list) {
        if (list == null) return;

        list.forEach(item -> {
            if (dataTableFieldStyleDao.queryBy(item.getId()) != null) {
                item.setUpdateTime(System.currentTimeMillis());
                dataTableFieldStyleDao.update(item);
            } else {
                item.setUpdateTime(System.currentTimeMillis());
                item.setCreateTime(System.currentTimeMillis());
                dataTableFieldStyleDao.insert(item);
            }
        });
    }

    @WriteTransactional
    public int delete(DataTableFieldStyleBO bo) {
        return dataTableFieldStyleDao.delete(bo);
    }

    @WriteTransactional
    public void deleteByFieldId(String fieldId) {
        DataTableFieldStyleBO qo = new DataTableFieldStyleBO();
        qo.setFieldId(fieldId);
        dataTableFieldStyleDao.queryList(qo).forEach(this::delete);
    }

    public List<DataTableFieldStyleBO> queryFieldId(String fieldId) {
        if (Tool.isBlank(fieldId)) {
            return Collections.emptyList();
        }

        DataTableFieldStyleBO qo = new DataTableFieldStyleBO();
        qo.setFieldId(fieldId);
        return dataTableFieldStyleDao.queryList(qo);
    }

    @WriteTransactional
    public void copyByFieldId(String oldFieldId, String newFieldId, String employeeId) {
        if (Tool.isBlank(oldFieldId) || Tool.isBlank(newFieldId)) {
            return;
        }

        DataTableFieldStyleBO qo = new DataTableFieldStyleBO();
        qo.setFieldId(oldFieldId);
        dataTableFieldStyleDao.queryList(qo).forEach(field -> {
            field.setId(UUID.getUUID());
            field.setFieldId(newFieldId);
            field.setUpdateId(employeeId);
            field.setCreateId(employeeId);
            field.setUpdateTime(System.currentTimeMillis());
            field.setCreateTime(System.currentTimeMillis());
            dataTableFieldStyleDao.insert(field);
        });
    }
}
